package server;



import gui.common.msg.MsgType;
import gui.common.msg.SysMessage;
import ibatis.SqlMapForumDao;
import ibatis.SqlMapGroupDao;
import ibatis.SqlMapMessageDao;
import ibatis.SqlMapUserDao;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.SQLException;
import java.util.List;

import client.CallBack;


import data.Count;
import data.Forum;
import data.Group;
import data.Message;
import data.User;
import data.dao.ForumDao;
import data.dao.GroupDao;
import data.dao.MessageDao;
import data.dao.UserDao;

public class DataServerImp extends UnicastRemoteObject implements DataServer{

	private static final long serialVersionUID = 1L;
	private UserDao usrDao=null;
	private ForumDao forumDao=null;
	private GroupDao groupDao=null;
	private MessageDao msgDao=null;

	private String welcomeInfo="Welcome to this";
	
	public String getWelcomeInfo() {
		return welcomeInfo;
	}
	
	
	public void setWelcomeInfo(String welcomeInfo) {
		this.welcomeInfo = welcomeInfo;
	}


	public DataServerImp() throws RemoteException{
		usrDao=new SqlMapUserDao();
		forumDao=new SqlMapForumDao();
		groupDao=new SqlMapGroupDao();
		msgDao=new SqlMapMessageDao();
	}


	public static void main(String[] args) {

		try {
			DataServerImp ds=new DataServerImp();
			Naming.rebind("rmi://localhost/DataServer", ds);
		} catch (Exception e) {
			System.out.println("DataServer err: " + e.getMessage());
			e.printStackTrace();
		}

	}

	public SysMessage addClientListener(CallBack callBackControl,
			String clientName) {
		SysMessage result=null;
		// TODO Auto-generated method stub
		return result;
	}

	public SysMessage createNewForum(Forum forum) throws RemoteException {
		SysMessage msg=null;
		try {
			msg=forumDao.createNewForum(forum);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return msg;
	}

	public SysMessage createNewGroup(Group group) throws RemoteException {
		SysMessage result=null;
		try {
			result=groupDao.createNewGroup(group);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	public SysMessage createNewMessage(Message msg) throws RemoteException {
		SysMessage result=null;
		try {
			result=msgDao.createNewMessage(msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public SysMessage createUser(String username,String password) throws RemoteException {
		SysMessage result=new SysMessage();
		result.subject="Create User";
		try {
			User usr=usrDao.addUser(username, password);
			if(usr!=null){
				result.type=MsgType.Success;
				result.data=usr;
				result.detail="User created:\n"+"name:"+usr.getUsername()+"\npassword"+usr.getPassword();
			}
		} catch (Exception e) {
			result.type=MsgType.Error;
			result.detail=e.getMessage();
			e.printStackTrace();
		}
		return result;
	}

	public SysMessage deleteForum(Forum forum) throws RemoteException {
		SysMessage result=null;
		try {
			result=forumDao.deleteForum(forum);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public SysMessage deleteGroup(Group group) throws RemoteException {
		SysMessage result=null;
		try {
			result=groupDao.deleteGroup(group);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public SysMessage deleteMessage(Message msg) throws RemoteException {
		SysMessage result=null;
		try {
			result=msgDao.deleteMessage(msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public SysMessage deleteUser(User user) throws RemoteException {
		SysMessage result=null;
		try {
			result=usrDao.deleteUser(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public Count getForumCount(Group group) throws RemoteException {
		Count result=null;
		try {
			result=forumDao.getForumCount(group);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public List<Forum> getForumList(Group group) throws RemoteException {
		List<Forum> result=null;
		try {
			result=forumDao.getForumList(group);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public List<Forum> getForumList(Group group, int start, int end){
		List<Forum> result=null;
		try {
			result=forumDao.getForumList(group,start,end);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public List<Group> getGroupList(boolean isAdmin) throws RemoteException {
		List<Group> result=null;
		try {
			result=groupDao.getGroupList(isAdmin);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public Count getMessageCount(Forum forum) throws RemoteException {
		Count result=null;
		try {
			result=msgDao.getMessageCount(forum);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public List<Message> getMessageList(Forum forum) throws RemoteException {
		List<Message> result=null;
		try {
			result=msgDao.getMessageList(forum);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public List<Group> getUserGroupRequest(User user) throws RemoteException{
		List<Group> result=null;
		try {
			result=groupDao.getUserGroupRequest(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public List<Group> getNewGroupRequest() throws RemoteException {
		List<Group> result=null;
		try {
			result=groupDao.getNewGroupRequests();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public List<Message> getPagedMessageList(Forum forum, int start, int end)
			throws RemoteException {
		List<Message> result=null;
		try {
			result=msgDao.getMesaList(forum, start, end);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	public SysMessage loginUser(String username,String password) throws RemoteException {

		User user=null;
		SysMessage sm=new SysMessage();
		try{
			user=usrDao.getUser(username,password);
			if(user==null){
				sm.subject="No such user";
				sm.type=MsgType.Warning;
			}else{
				sm.subject="Login success";
				sm.type=MsgType.Success;
			}
			sm.data=user;
		}catch (Exception e) {
			e.printStackTrace();
			sm.subject=e.getMessage();
			sm.type=MsgType.Error;
		}
		return sm;
	}

	public SysMessage updateForum(Forum forum) throws RemoteException {
		SysMessage result=null;
		try {
			result=forumDao.updateForum(forum);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public SysMessage updateGroup(Group group) throws RemoteException {

		SysMessage result=null;
		try {
			result=groupDao.updateGroup(group);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public SysMessage updateMessage(Message msg) throws RemoteException {
		SysMessage result=null;
		try {
			result=msgDao.updateMessage(msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public SysMessage updateUser(User user) throws RemoteException {
		SysMessage result=null;
		try {
			result=usrDao.updateUser(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}


	public List<User> getUserList() throws RemoteException {
		List<User> result=null;
		try {
			result=usrDao.getUserList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}


	

}
